package gold.digger;

import gold.utils.InputUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC853 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int carFleet(int target, int[] position, int[] speed) {
            int n = speed.length;
            if (n <= 1) {
                return n;
            }
            double[][] cars = new double[n][2];
            // 将速度换算成时间，更加直观，并且将速度和终点两个因素合并了；
            for (int i = 0; i < n; i++) {
                cars[i][0] = position[i];
                cars[i][1] = (double) (target - position[i]) / speed[i];
            }
            //升序排列
            Arrays.sort(cars, Comparator.comparingDouble(a -> a[0]));

            double maxTime = cars[n - 1][1];
            int ans = 1;
            for (int i = n - 2; i >= 0; i--) {
                //起点大的，时间又小（更快到终点），则单独作为一个车队  ans++
                //起点大的，时间长，会被起点小的追上，则与下一车作为同一车队
                if (cars[i][1] > maxTime) {
                    ans++;
                    maxTime = cars[i][1];
                }
            }

            return ans;
        }
    }


    public void run() {
        Solution solution = new Solution();
        int[] position = InputUtil.toIntegerArray("[10,8,0,5,3]");
        int[] speed = InputUtil.toIntegerArray("[2,4,1,1,3]");
        System.out.println(solution.carFleet(12, position, speed));
    }

    public static void main(String[] args) throws Exception {
        LC853 an = new LC853();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
